<template>
  <div class="comment">
    <div class="addcomment" v-show="!isfocus">
      <input type="text" placeholder="写跟帖" @focus="isfocus = true" />
      <span class="comment" @click="jump">
        <i class="iconfont iconpinglun-"></i>
        <em>{{ article.comment_length }}</em>
      </span>
      <i
        class="iconfont iconshoucang"
        :class="{ active: article.has_star }"
        @click="starThisPost"
      ></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment" v-show="isfocus">
      <textarea ref="commtext" rows="5" v-model.trim="content"></textarea>
      <div>
        <span @click="sendComment">发 送</span>
        <span  @click='cancel'>取 消</span>
        <!-- 取消实现的逻辑,第一简单就是我要把isfocus变为false 但是你点击回复
            了就会触发父组件@click="send(item)给子组件commentObj传值,commentObj的值有变化
            子组件的计算属性监听到你的变化触发了this.isfocus = true
            第二个就是你发布
            -->
      </div>
    </div>
  </div>
</template>

<script>
import { starPostById, pulishComment } from "@/apis/post";
export default {
  props: {
    article: {
      type: Object,
      require: true,
    },
    init: {
      type: Function,
    },
    // 用来接受父组件传递的评论的对象
    commentObj: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      isfocus: false,
      content: "",
    };
  },
  methods: {
    starThisPost() {
      starPostById(this.article.id).then((res) => {
        console.log(res);
        this.$toast.success(res.data.message);
        this.article.has_star = !this.article.has_star;
      });
    },
    jump() {
      this.$router
        .push({ path: `/comment/${this.article.id}` })
        .catch((err) => {});
    },
    sendComment() {
      if (this.content.length == 0) {
        this.$toast.fail("请输入内容");
        return;
      }
      let data = {
        content: this.content,
      };
      // 判断用户当前是否是回复评论--如果有这个对象，说明用户是单击了回复评论
      if (this.commentObj) {
        data.parent_id = this.commentObj.id;
      }
      pulishComment(this.article.id, data).then((res) => {
        console.log(res);
        if (res.data.message == "评论发布成功") {
          this.$toast.success("评论发布成功");
          this.content = "";
          this.isfocus = false;
          this.$emit("refresh");
        }
      });
    },
    cancel() {
      this.isfocus = false;
      // 将之前的对象清空，重置为null
      // 这个操作会报错：不能直接为props成员赋值，你不能改变props成员的值
      // 注意：但是你可以改变props成员的属性值
      // this.commentObj = null
      this.$emit("reset");
    },
  },
  watch: {
    commentObj() {
      console.log("子组件中的打印：", this.commentObj);
      // 让输入框弹出    
      // 让输入框弹出,只有用户真正的单击了回复，意味着有回复评论对象，才弹出输入框
      if (this.commentObj) {
        this.isfocus = true
      }
    },
  },
};
</script>

<style lang="less" scoped>
.comment {
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 0;
}
.inputcomment {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
}
.addcomment {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  background-color: #fff;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
  .active {
    color: red;
  }
}
</style>